package leetcode.hashstring;

import java.util.*;

/**
 * Created by zxc on 2018/10/8.
 * 设DNA序列只包含['A','C','G','T'] 4个字符的字符串，找到所有长度的且出现超过一次的字符串
 */
public class FindRepeateadDnaSequences {
    /*寻找重复字符串*/
    private static ArrayList<String> findRepeateadDnaSequences(String str) {
        HashMap<String,Integer> wordHash = new HashMap<>();
        ArrayList<String> arr = new ArrayList<>();
        String tmpWord = null;
        for(int i = 0;i < str.length()-9; i++) {
            tmpWord = str.substring(i,i+10);
            if (wordHash.containsKey(tmpWord)) {
                wordHash.put(tmpWord,wordHash.get(tmpWord) + 1);
            }else{
                wordHash.put(tmpWord,1);
            }
        }
        Iterator iter = wordHash.entrySet().iterator();
        while (iter.hasNext()){
            Map.Entry entry = (Map.Entry) iter.next();
            if((Integer) entry.getValue() > 1) {
                arr.add((String) entry.getKey());
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        String s = "AAAAACCCCCAAAAACCCCCAAAAAGGGTTT";

        /*执行函数*/
        ArrayList<String> strs = findRepeateadDnaSequences(s);
        for(int i = 0; i < strs.size();i++) {
            System.out.println(strs.get(i));
        }
        System.out.println("end");
    }
}
